package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.AnnCollect;

@Service
public class AnnCollectDAO extends BaseDAO<AnnCollect, Integer>{
	
	private static final String sql_findAnnCollect = " FROM AnnCollect annc WHERE annc.ac = ? AND annc.uc = ?";
	
	public RESULT findByMap(Map<String, String> map, PageBean pb){
		StringBuffer hql = new StringBuffer("FROM AnnCollect annc WHERE 1=1");

		List<Object> list = null;
		if (null != map) {
			list = new ArrayList<Object>();
			
			String title = map.get(Parm.TITLE);
			String oc = map.get(Parm.OC);
			String start = map.get(Parm.START);
			String end = map.get(Parm.END);
			String codes = map.get(Parm.CODES);
			String uc = map.get(Parm.UC);
			
			if (isNotNull(title) || isNotNull(oc) || isNotNull(start) || isNotNull(end) || isNotNull(codes) || isNotNull(codes)){
				hql.append(" AND annc.ac in (SELECT a.code FROM Announcement a WHERE a.title like ? AND a.oc = ? AND a.rt >= ? AND a.rt <= ? AND a.code in (?)");
				list.add("%" + title + "%");
				list.add(Integer.parseInt(oc));
				list.add(DateUtils.getDateByStr(start, "yyyy-MM-dd"));
				list.add(DateUtils.getDateByStr(end, "yyyy-MM-dd"));
				list.add(codes);		
			}

			if(isNotNull(uc)) {
				hql.append(" AND ac.uc = ?");
				list.add(Integer.parseInt(uc));	
			}
			
		}
		hql.append(" AND ac.deleted = 0 or ac.deleted is null ");
		hql.append(" ORDER BY ac.code  DESC");
		
		if (findToPageBean(hql.toString(), pb, list) == RESULT.SUCCESS)
			return RESULT.SUCCESS;
		
		return RESULT.ERROR;
	}
	public List<AnnCollect> findByCodeUser (Integer code, Integer uc){
		List<AnnCollect> ann = new ArrayList<AnnCollect>();
		ann = findI(sql_findAnnCollect, code, uc);
		return ann;
	}

}
